/*
 * Project: com.mmall.user.provider
 *
 * File Created at 2019-10-16
 *
 * Copyright 2019 CMCC Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ZYHY Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license.
 */
package com.mmall.product.provider.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.framework.bo.PageResultBO;
import com.common.framework.constant.DeleteStatus;
import com.common.framework.dto.PageDTO;
import com.mmall.common.spring.service.IDGenerateService;
import com.mmall.product.api.bo.ProductBO;
import com.mmall.product.api.bo.ProductDetailBO;
import com.mmall.product.api.constant.ProductStatus;
import com.mmall.product.api.dto.ProductDetailDTO;
import com.mmall.product.api.service.ProductService;
import com.mmall.product.provider.convert.ProductConvert;
import com.mmall.product.provider.convert.ProductDetailConvert;
import com.mmall.product.provider.dao.ProductMapper;
import com.mmall.product.provider.model.ProductDO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author jianghucoding
 * @date 2019-10-17
 * @Desc
 */
@Service(version = "1.0.0")
@Component
@Validated
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IDGenerateService idGenerateService;

    @Override
    public PageResultBO<ProductBO> findProducts(PageDTO pageDTO) {
        Page<ProductDO> page = new Page<>(pageDTO.getPage(), pageDTO.getSize());
        productMapper.pageQuery(page);
        return new PageResultBO<>(page.getTotal(), page
                .getRecords()
                .stream()
                .map(ProductConvert.INSTANCE::convert)
                .collect(Collectors.toList()));
    }

    @Override
    public ProductDetailBO detail(Long id) {
        ProductDO productDO = productMapper.selectById(id);
        return ProductDetailConvert.INSTANCE.convert(productDO);
    }

    @Override
    public void createProduct(Long userId, ProductDetailDTO productDetailDTO) {
        Long id = idGenerateService.generateID();
        ProductDO productDO = ProductDetailConvert.INSTANCE.convert(productDetailDTO);
        productDO.setUserId(userId).setStatus(ProductStatus.ACTIVE).setDeleted(DeleteStatus.ACTIVE).setId(id);
        productMapper.insert(productDO);
    }

    @Override
    public List<ProductBO> findProducts(Set<Long> productIds) {
        List<ProductDO> productDOS = productMapper.selectBatchIds(productIds);
        return ProductConvert.INSTANCE.convert(productDOS);
    }
}
